<?php
class pagination {
	function __construct($module, $totalRowsCount, $currentPage=null, $urlTemplate=null, $rowsPerPage=DEFAULT_ROWS_PER_PAGE) {
		$urlTemplate = ($urlTemplate === null)?$module->engine->getBaseURL()."%%P%%/":$urlTemplate;
		$module->engine->addTemplate(WIDGETS_TEMPLATES_PATH."pagination.xsl");
		
		$pNum = count($module->xml->pagination);
		$currentPage = (is_natural($currentPage) && $currentPage>0)?$currentPage:1;
		$pagesCount = ceil($totalRowsCount/$rowsPerPage);
		$module->xml->pagination[$pNum] = "";
		$module->xml->pagination[$pNum]['pagesCount'] = $pagesCount;
		
		$c = 0;
		for ($i=1; $i<=$pagesCount; $i++) {
			$module->xml->pagination[$pNum]->page[$c] = $i;
			if ($i == $currentPage) {
				$module->xml->pagination[$pNum]->page[$c]['active'] = 1;
			} else {
				$module->xml->pagination[$pNum]->page[$c]['url'] = str_replace('%%P%%', $i, $urlTemplate);
			}
			$c++;
		}
	}
}
?>